﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Text.RegularExpressions;

namespace TianHua.AutoCAD.Utility.ExtensionTools
{
    public static class TextTool
    {
        public static string GetRealTextString(this Cell cell)
        {
            if (cell.Value == null)
            {
                return "";
            }
            return cell.TextString.GetRealCADTextstring();
        }


        public static string GetRealCADTextstring(this string content)
        {
            //将多行文本按“\\”进行分割
            string[] strs = content.Split(new string[] { @"\\" }, StringSplitOptions.None);
            //指定不区分大小写
            RegexOptions ignoreCase = RegexOptions.IgnoreCase;
            for (int i = 0; i < strs.Length; i++)
            {
                //删除段落缩进格式
                strs[i] = Regex.Replace(strs[i], @"\\pi(.[^;]*);", "", ignoreCase);
                //删除制表符格式
                strs[i] = Regex.Replace(strs[i], @"\\pt(.[^;]*);", "", ignoreCase);
                //删除堆迭格式
                strs[i] = Regex.Replace(strs[i], @"\\S(.[^;]*)(\^|#|\\)(.[^;]*);", @"$1$3", ignoreCase);
                strs[i] = Regex.Replace(strs[i], @"\\S(.[^;]*)(\^|#|\\);", "$1", ignoreCase);
                //删除字体、颜色、字高、字距、倾斜、字宽、对齐格式
                strs[i] = Regex.Replace(strs[i], @"(\\F|\\C|\\H|\\T|\\Q|\\W|\\A)(.[^;]*);", "", ignoreCase);
                //删除下划线、删除线格式
                strs[i] = Regex.Replace(strs[i], @"(\\L|\\O|\\l|\\o)", "", ignoreCase);
                //删除不间断空格格式
                strs[i] = Regex.Replace(strs[i], @"\\~", "", ignoreCase);
                //删除换行符格式
                strs[i] = Regex.Replace(strs[i], @"\\P", "\n", ignoreCase);
                //删除换行符格式(针对Shift+Enter格式)
                //strs[i] = Regex.Replace(strs[i], "\n", "", ignoreCase);
                //删除{}
                strs[i] = Regex.Replace(strs[i], @"({|})", "", ignoreCase);
                //替换回\\,\{,\}字符
                //strs[i] = Regex.Replace(strs[i], @"\x01", @"\", ignoreCase);
                //strs[i] = Regex.Replace(strs[i], @"\x02", @"{", ignoreCase);
                //strs[i] = Regex.Replace(strs[i], @"\x03", @"}", ignoreCase);
            }
            return string.Join("\\", strs);//将文本中的特殊字符去掉后重新连接成一个字符串

        }

        // 文字包围框
        //  https://adndevblog.typepad.com/autocad/2013/10/mirroring-a-dbtext-entity.html
        public static void GetTextBoxCorners(
            this DBText dbText,
            out Point3d pt1,
            out Point3d pt2,
            out Point3d pt3,
            out Point3d pt4)
        {
            InvokeTool.ads_name name = new InvokeTool.ads_name();
            int result = InvokeTool.acdbGetAdsName(ref name, dbText.ObjectId);

            ResultBuffer rb = new ResultBuffer();
            Interop.AttachUnmanagedObject(rb,InvokeTool.acdbEntGet(ref name), true);

            // Call imported arx function
            double[] point1 = new double[3];
            double[] point2 = new double[3];
            InvokeTool.acedTextBox(rb.UnmanagedObject, point1, point2);
            pt1 = new Point3d(point1);
            pt2 = new Point3d(point2);

            // Create rotation matrix
            Matrix3d rotMat = Matrix3d.Rotation(dbText.Rotation, dbText.Normal, pt1);

            // The returned points from acedTextBox need
            // to be transformed as follow
            pt1 = pt1.TransformBy(rotMat).Add(dbText.Position.GetAsVector());
            pt2 = pt2.TransformBy(rotMat).Add(dbText.Position.GetAsVector());

            Vector3d rotDir = new Vector3d(
                -Math.Sin(dbText.Rotation), 
                Math.Cos(dbText.Rotation), 
                0);
            Vector3d linDir = rotDir.CrossProduct(dbText.Normal);
            double actualWidth = Math.Abs((pt2.GetAsVector() - pt1.GetAsVector()).DotProduct(linDir));
            pt3 = pt1.Add(linDir * actualWidth);
            pt4 = pt2.Subtract(linDir * actualWidth);
        }
    }
}
